import os
import time
from multiprocessing import Pipe, Process


def fun(name, fd1):
    time.sleep(3)
    # 向管道写入内容
    fd1.send({name: os.getpid()})


if __name__ == "__main__":
    # 单向管道。fd1只能读取消息，fd2只能写消息
    fd1, fd2 = Pipe(duplex=False)

    jobs = []
    for i in range(5):
        p = Process(target=fun, args=(i, fd2))
        jobs.append(p)
        p.start()
    for i in range(5):
        # 读管道
        data = fd1.recv()
        print(data)
    for i in jobs:
        i.join()
